﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace eSchoolbag.Utility
{
	static class Regeditor
	{
		static public string Read(string key, string name)
		{
			string registData;
			RegistryKey hkml = Registry.LocalMachine;
			RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
			RegistryKey aimdir = software.OpenSubKey(key, true);
			registData = aimdir.GetValue(name).ToString();
			return registData;
		}

		static public void Write(string key, string name, string value)
		{
			RegistryKey hklm = Registry.LocalMachine;
			RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
			RegistryKey aimdir = software.CreateSubKey(key);
			aimdir.SetValue(name, value);
		}

		static public void Delete(string key, string name)
		{
			string[] aimnames;
			RegistryKey hkml = Registry.LocalMachine;
			RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
			RegistryKey aimdir = software.OpenSubKey(key, true);
			aimnames = aimdir.GetSubKeyNames();
			foreach (string aimKey in aimnames)
			{
				if (aimKey == name)
					aimdir.DeleteSubKeyTree(name);
			}
		}

		static public bool IsExisted(string key, string name)
		{
			bool _exit = false;
			try
			{
				string[] subkeyNames;
				RegistryKey hkml = Registry.LocalMachine;
				RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
				RegistryKey aimdir = software.OpenSubKey(key, true);
				subkeyNames = aimdir.GetValueNames();
				foreach (string keyName in subkeyNames)
				{
					if (keyName == name)
					{
						_exit = true;
						return _exit;
					}
				}
			}
			catch
			{ }
			return _exit;
		}
	}
}
